home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-12-03 | 6.5 KB | 217 lines | [TEXT/PJMM] |
-
- unit stringWidthUnit;
- {}
- { LSP Project contains: }
- { XCMDIntf.p }
- { XCMDUtils.p }
- { Interface.lib }
- { DRVRRuntime.lib }
- { stringWidth.p (this file ) }
- {}
- { syntax is:stringWidth(stringHolder, font, size,}
- { style,<noDialog>) }
- { the parameters should be specified as hypercard }
- { reports them, ie. }
- { stringWidth("this is a dummy string", "PALATINO",}
- { "14", "BOLD,ITALIC", "noDialog") }
- {}
- { copyright (©) Eric Carlson and Jeremy Ahouse }
- { April 29, 1989 }
- { Waves Cosulting and Development }
- { Berkeley, CA 94792 }
- { free for non-commercial use only }
- {}
- interface
- uses
- HyperXcmd;
-
- procedure main (paramPtr: XCmdPtr);
-
- implementation
-
- {------------------------------------------------}
-
- procedure reportToUser (var paramPtr: XCmdPtr;
- msgStr: str255);
- {}
- { report something back to the user. we always fill }
- { in the result field of the paramBlock, and optionally }
- { use HC's "answer" dialog unless requested not to }
- {}
- var
- tempName: str255;
- begin
- paramPtr^.returnValue := PasToZero(paramPtr, msgStr);
- {check the last param to see if the user requested that}
- { we suppress the error dialog }
- ZeroToPas(paramPtr, paramPtr^.params[paramPtr^.paramCount]^, tempName);
- UprString(tempName, true);
- if tempName <> 'NODIALOG' then
- SendCardMessage(paramPtr, concat('answer "', msgStr, '"'));
- end; { procedure }
-
- function askedForHelp (var paramPtr: XCmdPtr;
- syntaxMsg: Str255;
- copyRightMsg: Str255): boolean;
- {}
- { check to see if the user sent a '?' or a '??' as }
- { the only parameter. if so we will respond with }
- { the calling syntax or the copyright/version info }
- { for this external }
- {}
- var
- firstStr: str255;
- begin
- askedForHelp := false;
- if paramPtr^.paramCount = 1 then
- begin
- ZeroToPas(paramPtr, paramPtr^.params[1]^, firstStr);
- { what is the first param? }
- if firstStr = '?' then
- begin
- reportToUser(paramPtr, syntaxMsg);
- askedForHelp := true
- end { asked for help }
- else if firstStr = '??' then
- begin
- reportToUser(paramPtr, copyRightMsg);
- askedForHelp := true
- end; { asked for copyright info }
- end; { one parameter passed }
- end; { function }
-
- procedure widthOfString (paramPtr: XCmdPtr);
- {}
- { set the specified pen characteristics and get the }
- { width of the string with the toolbox routine }
- { StringWidth }
- {}
- label
- 1;
- var
- passedString, errorStr, tempName: str255;
- copyRtStr, syntaxStr: str255;
- oldFont, oldSize, fNum, fSize, width: integer;
- fName, sizeString, theStyleStr: Str255;
- oldStyle, theStyle: Style;
- HCPort: GrafPtr;
- begin
- syntaxStr := 'stringWidth(stringHolder, font, size, style, <“noDialog”>)';
- { note the use of “smart quotes” so that }
- { HC doesn't choke if we use the answer }
- { dialog }
- copyRtStr := 'v1.0, ©1989 Waves Consulting and Development, Berkeley CA.';
- if paramPtr^.paramCount = 0 then
- begin
- { no parameters passed, report our calling syntax }
- reportToUser(paramPtr, syntaxStr);
- goto 1;
- end;
-
- if not (askedForHelp(paramPtr, syntaxStr, copyRtStr)) then
- begin
- GetPort(HCPort); { grab the port }
- with HCPort^ do
- begin
- oldFont := txFont; { save current typeface }
- oldSize := txSize; { save current size }
- oldStyle := txFace; { save current style }
- end;
-
- ZeroToPas(paramPtr, paramPtr^.params[1]^, passedString);{ get the string to trim }
-
- { do we have a font name parameter? }
- if paramPtr^.paramCount > 1 then
- ZeroToPas(paramPtr, paramPtr^.params[2]^, fName)
- { which font? }
- else
- fName := 'GENEVA';
- { no font passed, use HCs default }
-
- fNum := StrToNum(paramPtr, fName);
- { check to see if a number was passed as the font }
- {'name' parameter. if so, we assume that the font }
- { which HC wants to use for the field/button is not }
- { available in the current system. in this case geneva }
- { is being used instead, so we should use it too! }
- if fNum <> 0 then
- fName := 'GENEVA';
- GetFNum(fName, fNum); { get the font number }
- { if we call for an unavailable font (not present in }
- { this system, name spelled incorrectly, etc, GetFNum }
- { returns 0, which also happens to be the correct }
- { number for CHICAGO. thus we now check to see if }
- { the name for the font num is the same as the font }
- { name passed to us, or if our user is requesting the }
- { impossible }
- GetFontName(fNum, tempName);
- UprString(fName, true);
- UprString(tempName, true);
- if tempName <> fName then
- begin
- errorStr := concat('Sorry, the font ', chr(39), fName, chr(39), ' is not avaliable.');
- reportToUser(paramPtr, errorStr);
- goto 1;
- end;
-
- if paramPtr^.paramCount > 2 then
- { do we have a size parameter? }
- ZeroToPas(paramPtr, paramPtr^.params[3]^, sizeString) { font size in string form }
- else
- sizeString := '12';
- { no size passed, use HCs default }
- fSize := StrToNum(paramPtr, sizeString);
- { actual size }
-
- theStyle := [];
- { is there a style parameter? }
- if paramPtr^.paramCount > 3 then
- begin
- ZeroToPas(paramPtr, paramPtr^.params[4]^, theStyleStr); { which style(s)? }
- UprString(theStyleStr, true);
- { convert to uppercase }
-
- if pos('BOLD', theStyleStr) > 0 then
- theStyle := theStyle + [bold];
- if pos('ITALIC', theStyleStr) > 0 then
- theStyle := theStyle + [italic];
- if pos('UNDERLINE', theStyleStr) > 0 then
- theStyle := theStyle + [underline];
- if pos('OUTLINE', theStyleStr) > 0 then
- theStyle := theStyle + [outline];
- if pos('SHADOW', theStyleStr) > 0 then
- theStyle := theStyle + [shadow];
- if pos('CONDENSE', theStyleStr) > 0 then
- theStyle := theStyle + [condense];
- if pos('EXTEND', theStyleStr) > 0 then
- theStyle := theStyle + [extend];
- end;
-
- { now setup the port with the specified font }
- { attributes }
- TextFont(fNum); { set it to the current font, }
- TextSize(fSize); { and the size, }
- TextFace(theStyle); { and the style... }
-
- width := StringWidth(passedString);
- { how wide is that string? }
-
- { we mustn't forget to clean up after ourselves, }
- { reset HC's port to the entry conditions }
- TextFont(oldFont); { reset the font… }
- TextSize(oldSize); { and the size… }
- TextFace(oldStyle);{ and the style }
-
- { send back the width }
- NumToStr(paramPtr, width, tempName);
- paramPtr^.returnValue := PasToZero(paramPtr, tempName);
- end;
-
- 1: {bail out point if we run into trouble }
- end;
-
- procedure main;
- begin
- widthOfString(paramPtr);
- end;
- end.